import 'package:get/get.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:yt_devops/model/info_item.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/utils/enums.dart';

class AlarmDetailModel {
  /// 告警ID
  int? id;

  /// 设备ID
  int? deviceId;

  /// 父级ID
  int? parentId;

  /// 告警设备ID
  int? alarmDeviceId;

  /// 告警等级
  AlarmStatusGradeType? level;

  /// 告警名称
  String? name;

  /// 告警内容
  String? content;

  /// 告警时间
  String? alarmTime;

  /// 告警状态，是否已处理(清除) 0未处理 1已处理
  AlarmStatusType? status;

  /// 是否确认
  AlarmConformStatusType? isConfirm;

  /// 恢复类型 0设备自动恢复 1人为操作恢复
  int? recoverType;

  /// 修复时间
  String? recoveryTime;

  /// 站点名称
  String? siteName;

  /// 设备名称
  String? deviceName;

  /// 产品类型名称
  int? productType;

  /// 产品类型名称
  String? productTypeName;

  /// 产品ID
  int? productId;

  /// 站点ID
  int? siteId;

  /// 告警原因
  String? alarmCause;

  /// 告警来源
  int? fromResource;

  /// 修复建议
  String? repairSuggestion;

  /// 子系统ID
  int? subsystemId;

  /// 站点地址
  String? siteAddress;

  /// 角色ids
  List<dynamic>? roleIds;

  /// 页数
  int? pageNum;

  /// 每页分页数
  int? pageSize;

  /// 设备在线状态
  ConnectStatusType? connectStatus;

  AlarmDetailModel({
    this.id,
    this.deviceId,
    this.parentId,
    this.alarmDeviceId,
    this.level,
    this.name,
    this.content,
    this.alarmTime,
    this.status,
    this.isConfirm,
    this.recoverType,
    this.fromResource,
    this.recoveryTime,
    this.siteName,
    this.siteId,
    this.alarmCause,
    this.deviceName,
    this.productType,
    this.productTypeName,
    this.productId,
    this.repairSuggestion,
    this.subsystemId,
    this.siteAddress,
    this.roleIds,
    this.pageNum,
    this.pageSize,
    this.connectStatus,
  });

  factory AlarmDetailModel.fromJson(Map<String, dynamic> json) =>
      AlarmDetailModel(
        id: json['id'],
        deviceId: json['deviceId'],
        parentId: json['parentId'],
        alarmDeviceId: json['alarmDeviceId'],
        level: $enumDecodeNullable($AlarmStatusGradeTypeEnumMap, json['level']),
        name: json['name'],
        content: json['content'],
        alarmTime: json['alarmTime'],
        fromResource: json['fromResource'],
        status: $enumDecodeNullable($AlarmStatusTypeEnumMap, json['status']),
        isConfirm: $enumDecodeNullable(
            $AlarmConformStatusTypeEnumMap, json['isConfirm']),
        recoverType: json['recoverType'],
        recoveryTime: json['recoveryTime'],
        siteName: json['siteName'],
        siteId: json['siteId'],
        alarmCause: json['alarmCause'],
        deviceName: json['deviceName'],
        productId: json['productId'],
        productType: json['productType'],
        productTypeName: json['productTypeName'],
        repairSuggestion: json['repairSuggestion'],
        subsystemId: json['subsystemId'],
        siteAddress: json['siteAddress'],
        roleIds: json['roleIds'],
        pageNum: json['pageNum'],
        pageSize: json['pageSize'],
        connectStatus: $enumDecodeNullable(
            $ConnectStatusTypeEnumMap, json['connectStatus']),
      );

  List<InfoItem> get detailInfoLists {
    var source = '';
    if (fromResource.toString() == '0') {
      source = ResKey.deviceSide.tr;
    } else {
      source = ResKey.platformRules.tr;
    }
    if (status == AlarmStatusType.clear) {
      return [
        InfoItem('${ResKey.siteName.tr}:', siteName?.toString() ?? ''),
        InfoItem('${ResKey.alarmSource.tr}:', source),
        InfoItem('${ResKey.alarmLevel.tr}:', level?.label.toString() ?? ''),
        InfoItem('${ResKey.alarmStatus.tr}:', status?.label.toString() ?? ''),
        // InfoItem('${ResKey.alarmId.tr}:', id?.toString() ?? ''),
        InfoItem('${ResKey.deviceName.tr}:', deviceName?.toString() ?? ''),
        InfoItem('${ResKey.deviceType.tr}:', productTypeName?.toString() ?? ''),
        InfoItem('${ResKey.generationTime.tr}:', alarmTime?.toString() ?? ''),
        InfoItem(
            '${ResKey.eliminationTime.tr}:', recoveryTime?.toString() ?? ''),
      ];
    }
    return [
      InfoItem('${ResKey.siteName.tr}:', siteName?.toString() ?? ''),
      InfoItem('${ResKey.alarmSource.tr}:', source),
      InfoItem('${ResKey.alarmStatus.tr}:', status?.label.toString() ?? ''),
      // InfoItem('${ResKey.alarmId.tr}:', id?.toString() ?? ''),
      InfoItem('${ResKey.deviceName.tr}:', deviceName?.toString() ?? ''),
      InfoItem('${ResKey.deviceType.tr}:', productTypeName?.toString() ?? ''),
      InfoItem('${ResKey.generationTime.tr}:', alarmTime?.toString() ?? ''),
    ];
  }

  List<InfoItem> get alarmReasonList {
    return [
      InfoItem('告警原因内容', alarmCause ?? ''),
    ];
  }

  List<InfoItem> get repairSuggestionList {
    return [
      InfoItem('修复意见内容', repairSuggestion ?? ''),
    ];
  }

  List<InfoItem> get siteAddressList {
    return [
      InfoItem(ResKey.siteAddress.tr, siteAddress ?? ''),
    ];
  }
}

class AlarmDetailItem {
  String label;
  String value;

  AlarmDetailItem(this.label, this.value);
}
